/*
 *  Netzpult - Your desk on the web
 *  Copyright (C) 2011  Ulrich Hilger, http://uhilger.de
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see http://www.gnu.org/licenses/
 */

package de.uhilger.netzpult.client;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.FormPanel;
import com.google.gwt.user.client.ui.FormPanel.SubmitCompleteEvent;
import com.google.gwt.user.client.ui.FormPanel.SubmitEvent;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 
 * @author Copyright (c) Ulrich Hilger, http://uhilger.de
 * @author Published under the terms and conditions of
 * the <a href="http://www.gnu.org/licenses/" target="_blank">GNU General Public License</a>
 */
public class UploadDialog extends DialogBox {

  public UploadDialog() {
  	this("-1");
  }

	public UploadDialog(String id) {
		setHTML("Datei zu netzpult senden...");

	  // Create a FormPanel and point it at a service.
    final FormPanel form = new FormPanel();
    
		String appPath = Window.Location.getPath().substring(1);
		appPath = appPath.substring(0, appPath.indexOf("/"));
		
		//Window.alert("appPath=" + appPath); // liefert appPath=Netzpult
		
		if(appPath != null && appPath.length() > 0) {
	    form.setAction("/" + appPath + "/content/senden");
		} else {
	    form.setAction("/content/senden");
		}

    // Because we're going to add a FileUpload widget, we'll need to set the
    // form to use the POST method, and multipart MIME encoding.
    form.setEncoding(FormPanel.ENCODING_MULTIPART);
    form.setMethod(FormPanel.METHOD_POST);

    // Create a panel to hold all of the form widgets.
    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);

    // Create a TextBox, giving it a name so that it will be submitted.
    final TextBox tb = new TextBox();
    tb.setName("textBoxFormElement_length");
    tb.setText("length 0");
    panel.add(tb);

    final TextBox tbUpdateId = new TextBox();
    tbUpdateId.setName("textBoxFormElement_updateDocId");
    tbUpdateId.setText(id);
    panel.add(tbUpdateId);

    // Create a ListBox, giving it a name and some values to be associated with
    // its options.
    ListBox lb = new ListBox();
    lb.setName("listBoxFormElement_type");
    lb.addItem("JPEG-Bild", "image/jpeg");
    lb.addItem("PNG-Bild", "image/png");
    lb.addItem("ZIP-Archiv", "application/zip");
    panel.add(lb);

    // Create a FileUpload widget.
    FileUpload upload = new FileUpload();
    upload.setName("uploadFormElement");
    panel.add(upload);
    
    HorizontalPanel buttonPanel = new HorizontalPanel();

    // Add a 'cancel' button.
    buttonPanel.add(new Button("Abbrechen", new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    }));

    // Add a 'submit' button.
    buttonPanel.add(new Button("Senden", new ClickHandler() {
      public void onClick(ClickEvent event) {
        form.submit();
      }
    }));
    
    panel.add(buttonPanel);

    // Add an event handler to the form.
    form.addSubmitHandler(new FormPanel.SubmitHandler() {
      public void onSubmit(SubmitEvent event) {
        // This event is fired just before the form is submitted. We can take
        // this opportunity to perform validation.
        if (tb.getText().length() == 0) {
          Window.alert("Das Textfeld darf nicht leer sein");
          event.cancel();
        }
      }
    });
    form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
      public void onSubmitComplete(SubmitCompleteEvent event) {
        // When the form submission is successfully completed, this event is
        // fired. Assuming the service returned a response of type text/html,
        // we can get the result text here (see the FormPanel documentation for
        // further explanation).
        Window.alert(event.getResults());
        hide();
      }
    });

    add(form);
	}
	
}
